<?php

namespace app\admin\controller\projects\shop;

use app\Base;
use app\service\admin\projects\shop\GoodsService;
use app\validate\admin\GoodsValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * 商品管理
 * User : Du
 * Date : 2022/12/1
 * Time : 21:57
 * Class Goods
 * @package projects\controller\admin\shop
 */
class Goods extends Base
{

    /**
     * 商品
     * User : Du
     * Date : 2022/12/1
     * Time : 21:58
     * @param GoodsService $services
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function show(GoodsService $services): View
    {
        $res = $services::getClassify();
        return view('projects/shop/goods', ['data' => $res]);
    }

    /**
     * 列表数据
     * User : Du
     * Date : 2022/12/1
     * Time : 22:25
     * @param GoodsService $services
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(GoodsService $services): Json
    {
        $res = $services::getList($this->params);
        $this->res['data'] = $res['data'];
        $this->res['count'] = $res['count'];
        return $this->returnData();
    }

    /**
     * 详情
     * @param GoodsService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info(GoodsService $service): Json
    {
        $res = $service::info($this->params);
        if (!$res) {
            return $this->returnData('fail', '数据不存在');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 添加
     * @param GoodsService $services
     * @param GoodsValidate $validate
     * @return Json
     */
    public function add(GoodsService $services, GoodsValidate $validate): Json
    {
//        $res = $validate->check($this->params);
//        if (!$res) {
//            return $this->returnData('fail', $validate->getError());
//        }
        $res = $services::add($this->params);
        if (!$res) {
            return $this->returnData('addFail');
        }
        return $this->returnData();
    }

    /**
     * 修改
     * @param GoodsService $service
     * @return Json
     */
    public function update(GoodsService $service): Json
    {
        $res = $service::update($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }

    /**
     * 删除
     * @param GoodsService $service
     * @return Json
     */
    public function delete(GoodsService $service): Json
    {
        $res = $service::delete($this->params);
        if (!$res) {
            return $this->returnData('deleteFail');
        }
        return $this->returnData();
    }
}